package org.itboys.ace.web.lasted;

import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *作者：江雄
 * 描述：系统-字典
 * 时间：2017年2月24日.重新构建
 */
@RestController
@RequestMapping("/ace/system/dictionary")
public class AceSystemDictionaryController extends BaseController{
    @Autowired
    private SystemDictionaryService dictionaryService;

    @RequestMapping("/dictionary-list")
    public ModelAndView paramTemplateList(HttpServletRequest request, HttpServletResponse response, Model model) {
        return new ModelAndView("/ace/sys/dictionary-list");
    }

    /**
     * 分页查询字典
     * @param request
     * @param response
     */
    @RequestMapping("/selectDictionary")
    public void selectDictionary(HttpServletRequest request, HttpServletResponse response){

            PageResult<SystemDictionary> result=dictionaryService.selectDictionary(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);

    }
    /**
     * 删除
     * @param id
     * @param response
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public void delete(@PathVariable("id") Long id, HttpServletResponse response){
        try {
            dictionaryService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("SystemDictionary delete error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 保存
     * @param request
     * @param response
     */
    @RequestMapping(value="/saveDictionary", method=RequestMethod.POST)
    public void add(@ModelAttribute SystemDictionary sysDictionary, HttpServletRequest request, HttpServletResponse response) {
        try{
          dictionaryService.saveDictionary(sysDictionary);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }catch(Exception e){
            logger.error("SystemDictionary save error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 根据key，查询字典
     */
    @RequestMapping("/getDictionaryByKey")
    public void getDictionaryByKey(  @RequestParam(value="key",required=false) String key, HttpServletResponse response) {
        AjaxUtils.renderJson(response, dictionaryService.getDictionaryByKey(key));
    }
    @RequestMapping("/getDictionaryByKeys")
    public void getDictionaryByKeys(@RequestParam(value="keys",required=false) List<String> keys, HttpServletResponse response){
        AjaxUtils.renderJson(response, dictionaryService.getDictionaryByKeys(keys));
    }
}
